﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CFM.API;
using CFM.DTO;
using CFM.Common;

namespace CFM
{
    public partial class frmLogin : Form
    {        
        public frmLogin()
        {
            InitializeComponent();
        }

        private void btnConfig_Click(object sender, EventArgs e)
        {            
            frmConfig frm = new frmConfig();
            frm.ShowDialog();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            bool flag = true;
            if (txtUserId.Text.Length == 0)
            {
                MethodHelper.AddErrorProvider(txtUserId, Const.EMPTY_MESSAGE);
                flag = false;
            }
            if (txtPassword.Text.Length == 0)
            {
                MethodHelper.AddErrorProvider(txtPassword, Const.EMPTY_MESSAGE);
                flag = false;
            }
            if (flag)
            {
                MethodHelper.ClearErrorProvider();
                User user = (new UserAPI()).GetUserByIdAndPass(txtUserId.Text.Trim(), txtPassword.Text.Trim());
                if (string.IsNullOrEmpty(user.UserId))
                {
                    lblMessage.Text = "Tên đăng nhập hoặc mật khẩu sai";
                }
                else
                {
                    Const.CurrentUser = user;
                    Close();
                }
            }
        }

        private void txtPassword_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                btnLogin_Click(btnLogin, e);
            }
            else if (e.KeyValue == 27)
            {
                Application.Exit();
            }
        }

       
    }
}
